package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private long time = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //键盘按下的监听事件.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //退出键的代码
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(System.currentTimeMillis() - time > 2000){ //12101929112
                Toast.makeText(this, "在按就退出", Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
                return true;
            }else{
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public void send(View view) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题").setContentText("内容");
//        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));



        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);


        //锁屏通知
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);

        //样式
        builder.setDefaults(Notification.DEFAULT_ALL);
        //可弹出
        builder.setPriority(Notification.PRIORITY_MAX);


        Notification build = builder.build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(1,build);


    }

    public void send1(View view) {
        Notification.Builder mBuilder0 = new Notification.Builder(this);
        Notification.Builder mBuilder1 = new Notification.Builder(this);
        Notification.Builder mBuilder2 = new Notification.Builder(this);
        Notification.Builder mBuilder3 = new Notification.Builder(this);


        mBuilder0.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder1.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder2.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder3.setSmallIcon(R.mipmap.ic_launcher);


        mBuilder0.setContentTitle("标题1");
        mBuilder1.setContentTitle("标题2");
        mBuilder2.setContentTitle("标题3");
        mBuilder3.setContentTitle("标题4");

        mBuilder0.setContentText("内容1");
        mBuilder1.setContentText("内容2");
        mBuilder2.setContentText("内容3");
        mBuilder3.setContentText("内容4");

        mBuilder0.setGroup("1");

        mBuilder1.setGroup("1");
        mBuilder1.setGroupSummary(true);

        mBuilder2.setGroup("1");
        mBuilder3.setGroup("1");

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


        manager.notify(1,mBuilder0.build());
        manager.notify(2,mBuilder1.build());
        manager.notify(3,mBuilder2.build());
        manager.notify(4,mBuilder3.build());

    }

    public void send2(View view) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题");
//        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);

        //样式
        builder.setDefaults(Notification.DEFAULT_ALL);
        //可弹出
        builder.setPriority(Notification.PRIORITY_MAX);

//
//        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
//        inboxStyle.addLine("李白");
//        inboxStyle.addLine("露娜");
//        inboxStyle.addLine("韩信");

        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();

        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

        builder.setStyle(bigPictureStyle);




        Notification build = builder.build();




        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(1,build);


    }

    public void send3(View view) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题").setContentText("内容");
//        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setWhen(System.currentTimeMillis());
        builder.setAutoCancel(true);
        //样式
        builder.setDefaults(Notification.DEFAULT_ALL);
        //可弹出
        builder.setPriority(Notification.PRIORITY_MAX);


        //页面intent.
        Intent intent = new Intent(this, Main2Activity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);

        builder.setContentIntent(activity);

        Notification build = builder.build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        manager.notify(1,build);
    }

    public void send4(View view) {
//
//        Notification.Builder builder = new Notification.Builder(this);
//        builder.setSmallIcon(R.mipmap.ic_launcher);
//        builder.setContentTitle("标题").setContentText("内容1212121");
////        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
//
//        builder.setWhen(System.currentTimeMillis());
//        builder.setAutoCancel(true);
//
//
//        //样式
//        builder.setDefaults(Notification.DEFAULT_ALL);
//        //可弹出
//        builder.setPriority(Notification.PRIORITY_MAX);


//        Notification build = builder.build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //相同id为更新
//        manager.notify(1,build);

        manager.cancelAll();

    }
}
